//
//  ASSettingPassword.m
//  AttendenceSys
//
//  Created by 窦芳俊 on 16/2/18.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASSettingPassword.h"
#import "ASNetworkEngine.h"
#import "SVProgressHUD.h"

@interface ASSettingPassword ()

@end

@implementation ASSettingPassword

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setLeftButtonWithImageName:@"ic_back" bgImageName:nil];
    self.strNavTitle = @"修改密码";
    [self setRightText:@"保存"];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initView];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)initView
{
    UIImageView* bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    bgView.backgroundColor = [UIColor whiteColor];
    bgView.image = [UIImage imageNamed:@"bg_personal"];
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
    
    // oldPsdLabel
    _oldPsdLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, 130, 50)];
    //[_oldPsdLabel setBackgroundColor:[UIColor redColor]];
    [_oldPsdLabel setText:@"旧密码："];
    [_oldPsdLabel setTextColor:[UIColor blackColor]];
    [_oldPsdLabel setTextAlignment:NSTextAlignmentCenter];
    _oldPsdLabel.font = [UIFont fontWithName:@"Arial" size:18.0];
    [bgView addSubview:_oldPsdLabel];
    
    // _psdLabel
    _psdLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,
                                                          _oldPsdLabel.frame.origin.y + _oldPsdLabel.frame.size.height + 10,
                                                          130,
                                                          50)];
    //[_psdLabel setBackgroundColor:[UIColor redColor]];
    [_psdLabel setText:@"新密码："];
    [_psdLabel setTextColor:[UIColor blackColor]];
    [_psdLabel setTextAlignment:NSTextAlignmentCenter];
    _psdLabel.font = [UIFont fontWithName:@"Arial" size:18.0];
    [bgView addSubview:_psdLabel];
    
    // _psdAgainLabel
    _psdAgainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,
                                                               _psdLabel.frame.origin.y + _psdLabel.frame.size.height + 10,
                                                               130,
                                                               50)];
    //[_psdAgainLabel setBackgroundColor:[UIColor redColor]];
    [_psdAgainLabel setText:@"确认密码："];
    [_psdAgainLabel setTextColor:[UIColor blackColor]];
    [_psdAgainLabel setTextAlignment:NSTextAlignmentCenter];
    _psdAgainLabel.font = [UIFont fontWithName:@"Arial" size:18.0];
    [bgView addSubview:_psdAgainLabel];
    
    // _oldPsdField
    _oldPsdField = [[UITextField alloc] initWithFrame:CGRectMake(_oldPsdLabel.frame.origin.x + _oldPsdLabel.frame.size.width + 10,
                                                                 90,
                                                                 self.view.frame.size.width - (_oldPsdLabel.frame.origin.x + _oldPsdLabel.frame.size.width + 10) - 10,
                                                                 50)];
    _oldPsdField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _oldPsdField.textColor = [UIColor blackColor];
    _oldPsdField.returnKeyType = UIReturnKeyDone;
    [_oldPsdField setBackgroundColor:[UIColor clearColor]];
    _oldPsdField.placeholder = @"";
    _oldPsdField.delegate = self;
    _oldPsdField.font = [UIFont systemFontOfSize:18.0f];
    _oldPsdField.borderStyle = UITextBorderStyleRoundedRect;
    [bgView addSubview:_oldPsdField];
    
    // _psdField
    _psdField = [[UITextField alloc] initWithFrame:CGRectMake(_psdLabel.frame.origin.x + _psdLabel.frame.size.width + 10,
                                                              _oldPsdField.frame.origin.y + _oldPsdField.frame.size.height + 10,
                                                              self.view.frame.size.width - (_psdLabel.frame.origin.x + _psdLabel.frame.size.width + 10) - 10,
                                                              50)];
    _psdField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _psdField.textColor = [UIColor blackColor];
    _psdField.returnKeyType = UIReturnKeyDone;
    [_psdField setBackgroundColor:[UIColor clearColor]];
    _psdField.placeholder = @"";
    _oldPsdField.delegate = self;
    _psdField.font = [UIFont systemFontOfSize:18.0f];
    _psdField.borderStyle = UITextBorderStyleRoundedRect;
    [bgView addSubview:_psdField];
    
    // _psdAgainField
    _psdAgainField = [[UITextField alloc] initWithFrame:CGRectMake(_psdAgainLabel.frame.origin.x + _psdAgainLabel.frame.size.width + 10,
                                                        _psdField.frame.origin.y + _psdField.frame.size.height + 10,
                                                        self.view.frame.size.width - (_psdAgainLabel.frame.origin.x + _psdAgainLabel.frame.size.width + 10) - 10,
                                                        50)];
    _psdAgainField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _psdAgainField.textColor = [UIColor blackColor];
    _psdAgainField.returnKeyType = UIReturnKeyDone;
    [_psdAgainField setBackgroundColor:[UIColor clearColor]];
    _psdAgainField.placeholder = @"";
    _psdAgainField.delegate = self;
    _psdAgainField.font = [UIFont systemFontOfSize:18.0f];
    _psdAgainField.borderStyle = UITextBorderStyleRoundedRect;
    [bgView addSubview:_psdAgainField];
    
}

- (void)rightButtonPressed:(id)action
{
    [self saveNewPsd];
}

- (void)saveNewPsd
{
    //修改密码 http://211.65.216.54/modifyPassword.action?userCode=xiaohu&userPassword=ASADSASDf&newUserPassword=ASADSASDf&sourceTypeId=1 userCode学号 userPassword原始密码 newUserPassword新的密码 密码均MD5加密传给服务器端 sourceTypeId 1 老师 2 学生 3管理员
    
    NSString *oldPsd = _oldPsdField.text;
    NSString *newPsd = _psdField.text;
    NSString *newAgainPsd = _psdAgainField.text;
    //验证两次输入的新密码是否一致
    if (![newPsd isEqualToString:newAgainPsd])
    {
        [self.view rcsmakeToast:@"新密码不一致" duration:0.5f position:@"center"];
        return;
    }
    
    NSString *oldPsdMd5 = stringToMd5(oldPsd);
    NSString *newPsdMd5 = stringToMd5(newPsd);
    NSString *userCode = [ASAppDelegate sharedAppDelegate].userInfo.userCode;
    NSString *sourceTypeId = [ASAppDelegate sharedAppDelegate].userInfo.sourceTypeId;
    NSString* refer = @"/modifyPassword.action?";
    NSString* prefix = kASHttpUrl(refer);
    
    NSString* totalUrl = [NSString stringWithFormat:@"%@userCode=%@&userPassword=%@&newUserPassword=%@&sourceTypeId=%@",
                          prefix, userCode, oldPsdMd5, newPsdMd5, sourceTypeId];
    
    typeof(self) __weak weakSelf = self;
    [SVProgressHUD showWithStatus:@"处理中..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    [networkingEngine GET:totalUrl parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            //[SVProgressHUD dismiss];
            [SVProgressHUD showSuccessWithStatus:@"修改成功"];
            
            [weakSelf popToListVC];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

- (void)popToListVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITextFieldDelegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    
    return YES;
}


@end





